<?php
/**
 * soap服务端
 * 宁东服务器地址：http://10.217.84.158:28080/services/OutBoundService?wsdl
 */

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

Class SoapServiceController extends Controller
{
    function sendOutBoundData($param)
    {
        $getXml = Request()->getContent(); //file_get_contents("php://input");https://zhuanlan.zhihu.com/p/510814673
        info('风控请求xml参数:'.$getXml);
        try {
            //该转义的转义
            $getXml = str_replace(array("\r","\n"),'',$getXml);
            $getXml = str_replace('&lt;','<',$getXml);
            $getXml = str_replace('&gt;','>',$getXml);
            $getXml = str_replace('&#13;','',$getXml);
            //处理xml字符串
            $dom = new \DOMDocument();
            $dom->loadXML($getXml);
            $root = $dom->documentElement;
            $content = $dom->getElementsByTagName('param');
            $cdata = $content->item(0)->nodeValue;
            $Toolxml = json_encode(simplexml_load_string($cdata));
            $Toolxml = json_decode($Toolxml,true);
            $tool_list = $Toolxml['items']['item'];
            if(!$tool_list){
                $result_str ='<?xml version="1.0" encoding="UTF-8"?><o><resultValue><items class="array">';
                $result_str .= '解析后数据为空！</items></resultValue>';
                $result_str .= '<successful type="boolean">false</successful></o>';
                return array('return'=>$result_str);
            }
        }catch(\Exception $e) {
            $msg = "Exception error :" . $e->getMessage().' Line:'.$e->getLine();
            info($msg); //写入日志
            $result_str ='<?xml version="1.0" encoding="UTF-8"?><o><resultValue><items class="array">';
            $result_str .= $msg.'</items></resultValue>';
            $result_str .= '<successful type="boolean">false</successful></o>';
            return array('return'=>$result_str);
        }
        //存储获取到的参数
        info('解析后的param元素:'.json_encode($Toolxml));
        $third = new \App\Http\Controllers\Admin\ThirdController();
        $saveRes =  $third->autoData($tool_list);
        return array('return'=>$saveRes);

    }

    function send(){
        //测试列队上传风控数据
        $data_input_job = $_GET['code'];
        //通过编码来判断该样品是否已做完试验，做完试验上传数据到风控，若第一项不合格也上传数据
        $third_id = DB::table('a_third_data')->where('bar_code',$data_input_job)->orWhere('third_id2',$data_input_job)->value('third_id');
        $sample_info = DB::table('a_sample_info')->where('bar_code', $data_input_job)->orWhere('third_id', $data_input_job)->orderBy('id','desc')->first(); //山东国网工器具编号
        if(!$third_id || !$sample_info){
            Log::info('UploadGwDataJob:$third_id or $sample_info Missing Parameter');
            return false;
        }
        //判断是否做完，或者第一项实验项目不合格
        $res = DB::table('a_item_data')->where('sample_info_id',$sample_info->id)->get()->toArray();
        if(!$res){
            Log::info($data_input_job.'->该样品没有试验项目！！');
            return false;
        }
        $biaozhi = 0;   //标记是否全部完成变量
        $res_arr = array_values(array_column($res,'res','item_id'));
        if(in_array(0,$res_arr)){
            $biaozhi = 1;
        }elseif(in_array(2,$res_arr)){
            $biaozhi = 0;
        }else{
            $biaozhi = 1;
        }
        //调用上传功能
        if($biaozhi){
            //批次表状态修改为试验完成
            DB::table('a_contract')->where('id', $sample_info->contract_id)->where('status',0)->update(['status' =>8]);
            //code...
            $sample_ids = [$sample_info->id];
            $third_ids =[$third_id];
            request()->offsetSet('third_ids',$third_ids);
            request()->offsetSet('sample_ids',$sample_ids);
            $thirdCon = new \App\Http\Controllers\Admin\ThirdController();
            $uploadRes = $thirdCon->uploadRes(); //成功返回1，不成功返回错误信息
            Log::info('------------------上传结束，状态码：'.$uploadRes);
        }else{
            Log::info('样品编号：'.$data_input_job.' 尚未做完全部试验项目！！');
        }

    }
    function tjsendOutBoundData($param)
    {
        $getXml = Request()->getContent(); //file_get_contents("php://input");https://zhuanlan.zhihu.com/p/510814673
        info('风控请求xml参数:'.$getXml);
        try {
            //该转义的转义
            $getXml = str_replace(array("\r","\n"),'',$getXml);
            $getXml = str_replace('&lt;','<',$getXml);
            $getXml = str_replace('&gt;','>',$getXml);
            $getXml = str_replace('&#13;','',$getXml);
            //处理xml字符串
            $dom = new \DOMDocument();
            $dom->loadXML($getXml);
            $root = $dom->documentElement;
            $content = $dom->getElementsByTagName('param');
            $cdata = $content->item(0)->nodeValue;
            $Toolxml = json_encode(simplexml_load_string($cdata));
            $Toolxml = json_decode($Toolxml,true);
            $tool_list = $Toolxml['items']['item'];
            if(!$tool_list){
                $result_str ='<?xml version="1.0" encoding="UTF-8"?><o><resultValue><items class="array">';
                $result_str .= '解析后数据为空！</items></resultValue>';
                $result_str .= '<successful type="boolean">false</successful></o>';
                return array('return'=>$result_str);
            }
        }catch(\Exception $e) {
            $msg = "Exception error :" . $e->getMessage().' Line:'.$e->getLine();
            info($msg); //写入日志
            $result_str ='<?xml version="1.0" encoding="UTF-8"?><o><resultValue><items class="array">';
            $result_str .= $msg.'</items></resultValue>';
            $result_str .= '<successful type="boolean">false</successful></o>';
            return array('return'=>$result_str);
        }
        //存储获取到的参数
        info('解析后的param元素:'.json_encode($Toolxml));
        $third = new \App\Http\Controllers\Admin\ThirdController();
        $saveRes =  $third->autoData($tool_list);
        return array('return'=>$saveRes);

    }
}